#-*- coding: utf-8 -*-
'''
Created on 2010-4-6
$Revision: 5 $
$Id: convert.py 5 2010-04-06 09:14:06Z creesed $
$LastChangedDate: 2010-04-06 17:14:06 +0800 (星期二, 06 四月 2010) $
@description:
'''
import os
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from convertdlg import Ui_convertDlg

class convert(Ui_convertDlg,QDialog):
    def __init__(self,parent=None):
        super(convert,self).__init__(parent)
        self.setupUi(self)
        self.lock = QReadWriteLock()
        self.catiathread = catiaThread(self.lock,self)
        self.connect(self.catiathread, SIGNAL("msg(QString)"),self.displayMsg)
        self.connect(self.catiathread, SIGNAL("finished"),self.converted)
        self.updateListBox()
        
    @pyqtSignature("")
    def on_addFile_clicked(self):
        files = QFileDialog.getOpenFileNames(self,
                "Select Files:", ur"d:",
                "CATDrawing (*.CATDrawing)")
        for file in files:
            self.emit(SIGNAL("listChanged(QString)"),file)


    @pyqtSignature("")
    def on_delFile_clicked(self):
        self.fileList.takeTopLevelItem(self.fileList.indexOfTopLevelItem(self.fileList.currentItem()))
        
        
    @pyqtSignature("")
    def on_formatConvert_clicked(self):
        fileList = []
        for i in range(0,self.fileList.topLevelItemCount()):
            path = unicode(self.fileList.topLevelItem(i).text(2))
            name = unicode(self.fileList.topLevelItem(i).text(0))
            fileList.append(os.path.join(path,name)) 
        self.formatConvert.setEnabled(False)
        self.addFile.setEnabled(False)
        self.delFile.setEnabled(False)
        self.catiathread.initialize(fileList)
        self.catiathread.start()
        

    def displayMsg(self,msg):
        self.infoBox.insertPlainText(msg)
        self.addFile.setEnabled(True)
 
    def converted(self):
        self.addFile.setEnabled(True)
        self.delFile.setEnabled(True)
        self.formatConvert.setEnabled(True)
        self.infoBox.insertPlainText(u"全部转换完成!")
        self.formatConvert.setEnabled(1)
    
    def listUpdate(self,file):
        fullPath = os.path.realpath(file).decode('gb2312')
        fileName = os.path.basename(fullPath)
        filePath = os.path.dirname(fullPath)
        convertTo = ".pdf"
        item = QTreeWidgetItem([fileName,self.getSize(fullPath),filePath,convertTo])
        self.fileList.addTopLevelItem(item)
        self.fileList.setCurrentItem(item)
        self.updateListBox()
        
    def getSize(self,path):
        size = os.path.getsize(path)
        if size >= 1073741824 :
            return unicode(size/1073741824) + "GB"
        elif size >= 1048576:
            return unicode(size/1048576) + "MB"
        elif size >= 1024:
            return unicode(size/1024) + "KB"
        else :
            return unicode(size)+"B"
        
    def updateListBox(self):
        self.fileList.resizeColumnToContents(0)
        self.fileList.resizeColumnToContents(1)
        self.fileList.resizeColumnToContents(2)
        enable = self.fileList.topLevelItemCount()
        self.formatConvert.setEnabled(enable)
        self.delFile.setEnabled(enable)
        
class catiaThread(QThread):
    def __init__(self,lock,parent = None):
        super(catiaThread,self).__init__(parent)
        self.lock = lock
        self.app = None
    def initialize(self,fileList):
        self.fileList = fileList
    def run(self):
        import win32com.client
        catiaApp = win32com.client.Dispatch("Catia.Application")
        catiaApp.Visible = True
        for file in self.fileList:
            self.emit(SIGNAL("msg(QString)"),file+u"  正在打开...")
            catDoc = catiaApp.Documents.Open(file)
            self.emit(SIGNAL("msg(QString)"),u"正在转换...")
            outputfile = os.path.splitext(file)[0]+".PDF"
            catDoc.ExportData(outputfile,"pdf")
            self.emit(SIGNAL("msg(QString)"),u"Ok!\n")
            catDoc.close()

        self.emit(SIGNAL("finished"))
         
if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    dlg = convert()
    dlg.show()
    app.exec_()
    